/*
  Losu Copyright Notice
  --------------------
    Losu is an open source programming language project under the MIT license
  that can be used for both academic and commercial purposes. There are no
  fees, no royalties, and no GNU-like restrictions. Losu qualifies
  as open source software. However, Losu is not public property, and founder
  'chen-chaochen' retains its copyright.

    Losu has been registered with the National Copyright Administration of the
  People's Republic of China, and adopts the MIT license as the copyright
  licensing contract under which the right holder conditionally licenses its
  reproduction, distribution, and modification rights to an unspecified public.

    If you use Losu, please follow the public MIT agreement or choose to enter
  into a dedicated license agreement with us.

  The MIT LICENSE is as follows
  --------------------
  Copyright  2020  chen-chaochen

    Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the “Software”), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/

#include "../view.h"

#include "losu_mem.h"
#include "losu_object.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 1  // static

static void __hetu_style_calculate_size(losu_vm_t vm,
                                        losu_object_t obj,
                                        hetu_style_t style_out);
static void __hetu_style_calculate_pos(losu_vm_t vm,
                                       losu_object_t obj,
                                       hetu_style_t style_out);
static void __hetu_style_calculate_padding(losu_vm_t vm,
                                           losu_object_t obj,
                                           hetu_style_t style_out);
static void __hetu_style_calculate_margin(losu_vm_t vm,
                                          losu_object_t obj,
                                          hetu_style_t style_out);
static void __hetu_style_calculate_border(losu_vm_t vm,
                                          losu_object_t obj,
                                          hetu_style_t style_out);
static void __hetu_style_calculate_color(losu_vm_t vm,
                                         losu_object_t obj,
                                         hetu_style_t style_out);
static void __hetu_style_calculate_visibility(losu_vm_t vm,
                                              losu_object_t obj,
                                              hetu_style_t style_out);
static void __hetu_style_calculate_display(losu_vm_t vm,
                                           losu_object_t obj,
                                           hetu_style_t style_out);
static void __hetu_style_calculate_display_flex(losu_vm_t vm,
                                                losu_object_t obj,
                                                hetu_style_t style_out);

#endif

/**
 * @brief 计算样式
 * @param vm losu vm handle
 * @param obj losu object
 * @param style style handle
 * @return
 */
losu_extern_t void hetu_style_calculate(losu_vm_t vm,
                                        losu_object_t obj,
                                        hetu_style_t style) {
  if (!style)
    return;
  // size
  __hetu_style_calculate_size(vm, obj, style);
  // pos
  __hetu_style_calculate_pos(vm, obj, style);
  // padding
  __hetu_style_calculate_padding(vm, obj, style);
  // margin
  __hetu_style_calculate_margin(vm, obj, style);
  // border
  __hetu_style_calculate_border(vm, obj, style);
  // color
  __hetu_style_calculate_color(vm, obj, style);
  // visibility
  __hetu_style_calculate_visibility(vm, obj, style);
  // display
  __hetu_style_calculate_display(vm, obj, style);
  if (style->display.type == HETU_STYLE_VALUE_TYPE_ENUM) {
    if (style->display.value.enums == HETU_STYLE_DISPLAY_FLEX)
      __hetu_style_calculate_display_flex(vm, obj, style);
  }
}

losu_extern_t void hetu_style_eval(losu_vm_t vm,
                                   losu_object_t vobj,
                                   hetu_style_value_t value_out,
                                   hetu_style_exp_t exps) {
  // string: 1px 20% rba(r,g,b) rgba(r,g,b,a)  xxx
  (void)vm;
  switch (ovtype(vobj)) {
    case losu_object_type_string: {
      const char* s = ovSstr(vobj);
      size_t len = strlen(s);
      // third
      for (size_t i = 0; exps && exps[i].name; i++) {
        if (strcmp(exps[i].name, s) == 0) {
          value_out->type = HETU_STYLE_VALUE_TYPE_ENUM;
          value_out->value.enums = exps[i].value;
          return;
        }
      }
      // px
      if (len > 2 && strcmp(s + len - 2, "px") == 0) {
        uint16_t px = 0;
        sscanf(s, "%hupx", &px);
        value_out->type = HETU_STYLE_VALUE_TYPE_PX;
        value_out->value.px = px;
        return;
      }
      // percent
      if (len > 1 && strcmp(s + len - 1, "%") == 0) {
        uint16_t percent = 0;
        sscanf(s, "%hu%%", &percent);
        value_out->type = HETU_STYLE_VALUE_TYPE_PERCENT;
        value_out->value.percent = percent;
        return;
      }
      // rgb()
      if (strncmp(s, "rgb(", 4) == 0) {
        uint8_t r = 0xff, g = 0xff, b = 0xff;
        sscanf(s, "rgb(%hhu,%hhu,%hhu)", &r, &g, &b);
        value_out->type = HETU_STYLE_VALUE_TYPE_ARGB;
        value_out->value.argb = (hetu_style_ARGB_t){
            .a = 0xff,
            .r = r,
            .g = g,
            .b = b,
        };
        return;
      }
      // rgba()
      if (strncmp(s, "rgba(", 5) == 0) {
        uint8_t r = 0xff, g = 0xff, b = 0xff;
        float a = 0;
        sscanf(s, "rgba(%hhu,%hhu,%hhu,%f)", &r, &g, &b, &a);
        value_out->type = HETU_STYLE_VALUE_TYPE_ARGB;
        value_out->value.argb = (hetu_style_ARGB_t){
            .a = (uint8_t)(a * 255),
            .r = r,
            .g = g,
            .b = b,
        };
        return;
      }
      // # color
      if (s[0] == '#') {
        char name[len];
        sscanf(s, "#%s", name);
        for (size_t i = 0; i < nhetu_style_inlinecolors; i++) {
          if (strcmp(name, hetu_style_inlinecolors[i].name) == 0) {
            value_out->type = HETU_STYLE_VALUE_TYPE_ARGB;
            value_out->value.argb = hetu_style_inlinecolors[i].color;
            return;
          }
        }
      }
      // default
      value_out->type = HETU_STYLE_VALUE_TYPE_STRING;
      losu_objstring_newconst(vm, s);
      value_out->value.str = s;
      break;
    }
    case losu_object_type_number: {
      value_out->type = HETU_STYLE_VALUE_TYPE_NUMBER;
      value_out->value.number = ovnumber(vobj);
      break;
    }
    default: {
      break;
    }
  }
}

#if 1  // static

static void __hetu_style_calculate_size(losu_vm_t vm,
                                        losu_object_t obj,
                                        hetu_style_t style_out) {
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "width"), &style_out->width,
                  NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "height"), &style_out->height,
                  NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "min-width"),
                  &style_out->min_width, NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "min-height"),
                  &style_out->min_height, NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "max-width"),
                  &style_out->max_width, NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "max-height"),
                  &style_out->max_height, NULL);
}

static void __hetu_style_calculate_pos(losu_vm_t vm,
                                       losu_object_t obj,
                                       hetu_style_t style_out) {
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "x"), &style_out->x, NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "y"), &style_out->y, NULL);
}

static void __hetu_style_calculate_padding(losu_vm_t vm,
                                           losu_object_t obj,
                                           hetu_style_t style_out) {
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "padding-left"),
                  &style_out->padding_left, NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "padding-right"),
                  &style_out->padding_right, NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "padding-top"),
                  &style_out->padding_top, NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "padding-bottom"),
                  &style_out->padding_bottom, NULL);
}

static void __hetu_style_calculate_margin(losu_vm_t vm,
                                          losu_object_t obj,
                                          hetu_style_t style_out) {
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "margin-left"),
                  &style_out->margin_left, NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "margin-right"),
                  &style_out->margin_right, NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "margin-top"),
                  &style_out->margin_top, NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "margin-bottom"),
                  &style_out->margin_bottom, NULL);
}

static void __hetu_style_calculate_border(losu_vm_t vm,
                                          losu_object_t obj,
                                          hetu_style_t style_out) {
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "border-width"),
                  &style_out->border_width, NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "border-color"),
                  &style_out->border_color, NULL);
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "border-radius"),
                  &style_out->border_radius, NULL);
}
static void __hetu_style_calculate_color(losu_vm_t vm,
                                         losu_object_t obj,
                                         hetu_style_t style_out) {
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "background-color"),
                  &style_out->background_color, NULL);
  // hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "color"), &style_out->color,
  //                 NULL);
}
static void __hetu_style_calculate_visibility(losu_vm_t vm,
                                              losu_object_t obj,
                                              hetu_style_t style_out) {
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "visibility"),
                  &style_out->visibility,
                  (hetu_style_exp[]){
                      {"visible", HETU_STYLE_VISIBILITY_VISIBLE},
                      {"hidden", HETU_STYLE_VISIBILITY_HIDDEN},
                      {NULL, 0},
                  });
}
static void __hetu_style_calculate_display(losu_vm_t vm,
                                           losu_object_t obj,
                                           hetu_style_t style_out) {
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "display"),
                  &style_out->display,
                  (hetu_style_exp[]){
                      {"free", HETU_STYLE_DISPLAY_FREE},
                      {"flex", HETU_STYLE_DISPLAY_FLEX},
                      {NULL, 0},
                  });
}
static void __hetu_style_calculate_display_flex(losu_vm_t vm,
                                                losu_object_t obj,
                                                hetu_style_t style_out) {
  // flex-direction
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "flex-direction"),
                  &style_out->flex_direction,
                  (hetu_style_exp[]){
                      {"row", HETU_STYLE_FLEX_DIRECTION_ROW},
                      {"column", HETU_STYLE_FLEX_DIRECTION_COLUMN},
                      {NULL, 0},
                  });
  // flex-grow
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "flex-grow"),
                  &style_out->flex_grow, NULL);
  // align-items
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "align-items"),
                  &style_out->align_items,
                  (hetu_style_exp[]){
                      {"start", HETU_STYLE_ALIGN_ITEMS_START},
                      {"end", HETU_STYLE_ALIGN_ITEMS_END},
                      {"center", HETU_STYLE_ALIGN_ITEMS_CENTER},
                      {"baseline", HETU_STYLE_ALIGN_ITEMS_BASELINE},
                      {"stretch", HETU_STYLE_ALIGN_ITEMS_STRETCH},
                      {NULL, 0},
                  });
  // align-content
  hetu_style_eval(vm, obj_unitindexstr(vm, *obj, "align-content"),
                  &style_out->align_content,
                  (hetu_style_exp[]){
                      {"start", HETU_STYLE_ALIGN_CONTENT_START},
                      {"end", HETU_STYLE_ALIGN_CONTENT_END},
                      {"center", HETU_STYLE_ALIGN_CONTENT_CENTER},
                      {"space-between", HETU_STYLE_ALIGN_CONTENT_SPACE_BETWEEN},
                      {"space-around", HETU_STYLE_ALIGN_CONTENT_SPACE_AROUND},
                      {NULL, 0},
                  });
  // justify-content
  hetu_style_eval(
      vm, obj_unitindexstr(vm, *obj, "justify-content"),
      &style_out->justify_content,
      (hetu_style_exp[]){
          {"start", HETU_STYLE_JUSTIFY_CONTENT_START},
          {"end", HETU_STYLE_JUSTIFY_CONTENT_END},
          {"center", HETU_STYLE_JUSTIFY_CONTENT_CENTER},
          {"space-between", HETU_STYLE_JUSTIFY_CONTENT_SPACE_BETWEEN},
          {"space-around", HETU_STYLE_JUSTIFY_CONTENT_SPACE_AROUND},
          {NULL, 0},
      });
}
#endif